package com.dede.monochromatism.service

import android.os.Build
import android.preference.PreferenceManager
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import android.support.annotation.RequiresApi
import com.dede.monochromatism.S_PRE_MONO
import com.dede.monochromatism.util.Util

/**
 * @author hsh
 * @date 2017/9/28 17:06.
 * @doc
 */
@RequiresApi(Build.VERSION_CODES.N)
class MonoTileService : TileService() {

    override fun onStartListening() {
        super.onStartListening()
        var state = Util.getSimulateColorSpaceState(applicationContext)
        qsTile.state = if (state) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE
        qsTile.updateTile()
    }

    override fun onClick() {
        super.onClick()
        val state = Util.getSimulateColorSpaceState(applicationContext)
        val result = Util.setSimulateColorSpaceState(applicationContext, !state)
        if (result) {
            val sp = PreferenceManager.getDefaultSharedPreferences(applicationContext)
            sp.edit().putBoolean(S_PRE_MONO, !state).apply()
            qsTile.state = if (!state) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE
            qsTile.updateTile()
        }
    }
}